<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
<meta name="viewport"
      content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">

    <meta name="author" content="孟建都">


    <meta name="subtitle" content="孟建都的博客">


    <meta name="description" content="孟建都的博客">


    <meta name="keywords" content="孟建都,博客">


<title>vue跳转之后保存查询的数据（浏览器返回能够保存查询的条件） | 孟建都的博客</title>



    <link rel="icon" href="/blog/favicon.ico">




    <!-- stylesheets list from _config.yml -->
    
    <link rel="stylesheet" href="/blog/css/style.css">
    



    <!-- scripts list from _config.yml -->
    
    <script src="/blog/js/script.js"></script>
    
    <script src="/blog/js/tocbot.min.js"></script>
    



    
    
        
    


<meta name="generator" content="Hexo 5.1.1"></head>
<body>
    <div class="wrapper">
        <header>
    <nav class="navbar">
        <div class="container">
            <div class="navbar-header header-logo"><a href="/blog/">Jarrett&#39;s Blog</a></div>
            <div class="menu navbar-right">
                
                    <a class="menu-item" href="/blog/archives">文章</a>
                
                    <a class="menu-item" href="/blog/about">关于</a>
                
                <input id="switch_default" type="checkbox" class="switch_default">
                <label for="switch_default" class="toggleBtn"></label>
            </div>
        </div>
    </nav>

    
    <nav class="navbar-mobile" id="nav-mobile">
        <div class="container">
            <div class="navbar-header">
                <div>
                    <a href="/blog/">Jarrett&#39;s Blog</a><a id="mobile-toggle-theme">·&nbsp;Light</a>
                </div>
                <div class="menu-toggle" onclick="mobileBtn()">&#9776; Menu</div>
            </div>
            <div class="menu" id="mobile-menu">
                
                    <a class="menu-item" href="/blog/archives">文章</a>
                
                    <a class="menu-item" href="/blog/about">关于</a>
                
            </div>
        </div>
    </nav>

</header>
<script>
    var mobileBtn = function f() {
        var toggleMenu = document.getElementsByClassName("menu-toggle")[0];
        var mobileMenu = document.getElementById("mobile-menu");
        if(toggleMenu.classList.contains("active")){
           toggleMenu.classList.remove("active")
            mobileMenu.classList.remove("active")
        }else{
            toggleMenu.classList.add("active")
            mobileMenu.classList.add("active")
        }
    }
</script>
        <div class="main">
            <div class="container">
    
    

    
    <article class="post-wrap">
        <header class="post-header">
            <h1 class="post-title">vue跳转之后保存查询的数据（浏览器返回能够保存查询的条件）</h1>
            
                <div class="post-meta">
                    
                        Author: <a itemprop="author" rel="author" href="/">孟建都</a>
                    

                    
                        <span class="post-time">
                        Date: <a href="#">九月 7, 2020&nbsp;&nbsp;10:49:07</a>
                        </span>
                    
                    
                </div>
            
        </header>

        <div class="post-content">
            <blockquote>
<p>vue跳转之后再点击浏览器返回之后会清除查询的数据。我找到了两种不清除数据的方式</p>
</blockquote>
<ul>
<li>（不推荐）在router-view标签外面套一层keep-alive标签，这个标签能够使vue得组件不销毁，并且能够保存当前的状态，但是我并不推荐这种方式，组件的复用会出现问题。<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">keep-alive</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">router-view</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">keep-alive</span>&gt;</span></span><br></pre></td></tr></table></figure></li>
<li>（推荐）使用sessionStorage保存当前的查询，当vue组件销毁的时候调用sessionStorage.save方法保存查询的条件数据，当vue组件创建的时候调用sessionStorage.getItem方法重新获取查询的条件数据。<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line">saveParams() &#123;</span><br><span class="line">    sessionStorage.setItem(<span class="string">&#x27;article-params&#x27;</span>, <span class="built_in">JSON</span>.stringify(<span class="built_in">this</span>.params))</span><br><span class="line">&#125;,</span><br><span class="line">getParams() &#123;</span><br><span class="line">  <span class="keyword">const</span> params = <span class="built_in">JSON</span>.parse(sessionStorage.getItem(<span class="string">&#x27;article-params&#x27;</span>))</span><br><span class="line">  <span class="keyword">if</span> (params) &#123;</span><br><span class="line">    <span class="built_in">this</span>.params = params</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></li>
</ul>

        </div>

        
            <section class="post-copyright">
                
                    <p class="copyright-item">
                        <span>Author:</span>
                        <span>孟建都</span>
                    </p>
                
                
                
                

            </section>
        
        <section class="post-tags">
            <div>
                <span>Tag(s):</span>
                <span class="tag">
                    
                </span>
            </div>
            <div>
                <a href="javascript:window.history.back();">back</a>
                <span>· </span>
                <a href="/blog/">home</a>
            </div>
        </section>
        <section class="post-nav">
            
                <a class="prev" rel="prev" href="/blog/2020/09/07/Vue%E4%B8%AD%E4%BD%BF%E7%94%A8Async%E5%92%8CAwait%E4%BB%A3%E6%9B%BFPromise%E7%9A%84then%E6%96%B9%E6%B3%95/">Vue中使用Async和Await代替Promise的then方法</a>
            
            
            <a class="next" rel="next" href="/blog/2020/09/07/SpringBoot%E9%A1%B9%E7%9B%AE%E5%9C%A8Url%E4%B8%8A%E9%9D%A2%E5%87%BA%E7%8E%B0%7B%7D%5B%5D%E7%AD%89%E5%AD%97%E7%AC%A6%E5%90%8E%E6%8A%A5%E9%94%99/">SpringBoot项目在Url上面出现{}[]等字符后报错</a>
            
        </section>


    </article>
</div>

        </div>
        <footer id="footer" class="footer">
    <div class="copyright">
        <span>© 孟建都 | Powered by <a href="https://hexo.io" target="_blank">Hexo</a> & <a href="https://github.com/Siricee/hexo-theme-Chic" target="_blank">Chic</a></span>
    </div>
</footer>

    </div>
</body>
</html>
